<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 雲溪荏苒 <290648237@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

/**
 * 网站案例模型
 * Class SiteCase
 * @package app\common\model
 * @author 雲溪荏苒 <290648237@qq.com>
 * @date 2017/11/18
 */
class SiteCase extends Model{


    /**
     * 获取网站案例列表
     *
     * @param array $condition
     * @param int $curPage
     * @param int $pageSize
     * @param string $order
     * @param string $field
     * @return array
     */
    public function getSiteCaseList($condition =[], $curPage = 0, $pageSize = 10, $order='sort asc', $field='*')
    {
        if (!$curPage) {
            $curPage = request()->param('cur_page', 1);// 获取当前页码
        }
        $count = M('site_case')->field($field)->where($condition)->count();
        $result = M('site_case')->field($field)->where($condition)->page($curPage, $pageSize)->order($order)->select();
        foreach ($result as $k => $v) {
            $industry_name = M('site_industry')->where('id', $v['industry_id'])->value('industry_name');
            $result[$k]['industry_name'] = $industry_name;
        }
        return [$result, $count];
    }

    public function getSiteCaseByCate($classLimit = 0, $caseLimit = 0, $isAll = false)
    {
        // 获取全部案例
        if ($isAll) {
            $caseList = db('site_case')->where(['status' => 1])->order('sort asc')->limit($caseLimit)->select();
        } else {
            // 获取产品分类列表
            $siteIndustryList = db('site_industry')
                ->where('status', 1)
                ->order('sort asc')
                ->limit(0,$classLimit)
                ->select();
            $caseList = $siteIndustryList;
            if(!empty($caseList)) {
                foreach ($caseList as $key=>$item) {
                    $caseList[$key]['cases'] = db('site_case')->where(['status'=>1, 'industry_id'=>$item['id']])->order('sort asc')->limit(0,$caseLimit)->select();
                }
            }
        }

        return $caseList;
    }


    /**
     * 访问量+1
     * @param $id
     * @throws \think\Exception
     */
    public static function setViewInc($id)
    {
        db('site_case')->where('id', $id)->setInc('view');
    }

}














